<template>
  <el-dialog
    title="提示"
    :visible="visible"
    width="50%"
    @close="clickClose"
  >
    <el-form :model="form" :rules="rules" label-width="100px">
      <el-form-item prop="name" label="部门名称">
        <el-input v-model="form.name" placeholder="请输入部门名称" />
      </el-form-item>
      <el-form-item prop="code" label="部门编码">
        <el-input v-model="form.code" placeholder="请输入部门编码" />
      </el-form-item>
      <el-form-item prop="managerId" label="部门负责人">
        <el-input v-model="form.managerId" placeholder="请输入部门负责人" />
      </el-form-item>
      <el-form-item prop="introduce" label="部门介绍">
        <el-input v-model="form.introduce" placeholder="请输入部门介绍" type="textarea" rows="4" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="mini" type="primary" @click="clickClose">确 定</el-button>
      <el-button size="mini" @click="clickClose">取 消</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    list: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      form: {
        name: '',
        code: '',
        managerId: '',
        introduce: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: ['blur', 'change'] },
          { min: 2, max: 10, message: '长度应该在2-10位', trigger: ['blur', 'change'] },
          { validator: (rule, value, cb) => {
            this.list.find(item => item.name === value) ? cb(new Error('该部门已存在')) : cb()
          },
          trigger: ['blur', 'change'] }
        ],
        code: [
          { required: true, message: '请输入部门编码', trigger: ['blur', 'change'] },
          { min: 2, max: 10, message: '长度应该在2-10位', trigger: ['blur', 'change'] },
          { validator: (rule, value, cb) => {
            this.list.find(item => item.code === value) ? cb(new Error('该部门编码已存在')) : cb()
          },
          trigger: ['blur', 'change'] }
        ],
        managerId: [
          { required: true, message: '请输入部门负责人', trigger: ['change'] }
        ],
        introduce: [
          { required: true, message: '请输入部门介绍', trigger: ['blur', 'change'] },
          { min: 2, max: 100, message: '长度应该在2-10位', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  methods: {
    clickClose() {
      this.$emit('update:visible', false)
    }
  }
}
</script>

<style lang='scss' scoped>
::v-deep .el-dialog__footer{
  text-align: center;
}

</style>
